package com.group.nepm.statisticDataManagement.controller;

import com.group.common.utils.MyUtils;
import com.group.nepg.entity.AccurateAqiData;
import com.group.nepm.statisticDataManagement.entity.StatisticData;
import com.group.neps.entity.RowAqiData;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;

import java.util.ArrayList;

public class OtherDataStatisticController {
    @FXML
    Label numberCountLabel;
    @FXML
    Label goodNumberCountLabel;
    @FXML
    Label provinceLabel;
    @FXML
    Label cityLabel;

    public void adminOut(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/common/view/NEPMLoginView.fxml");
    }
    public void toPublicSuperviseDataList(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/publicSuperviseDataManagement/view/PublicSuperviseDataList.fxml");
    }
    public void toConfirmAqiDataList(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/publicSuperviseDataManagement/view/ConfirmAqiDataList.fxml");
    }
    public void toProvinceStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/provinceStatistic.fxml");
    }
    public void toAqiDistributionStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/aqiDistributionStatistic.fxml");
    }
    public void toAqiTendencyStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/aqiTendencyStatistic.fxml");
    }
    public void toOtherDataStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/otherDataStatistic.fxml");
    }
    public void initialize() {
        String statisticDataPath = "group/data/statistic_data_arraylist.dat";
        ArrayList<StatisticData> statisticDataList = MyUtils.readFileToArrayList(statisticDataPath);

        if (statisticDataList == null) {
            System.out.println("无法读取数据文件！");
            return;
        }

        int totalCount = statisticDataList.size();
        int goodAqiCount = getGoodAqiCount(statisticDataList);

        numberCountLabel.setText(String.valueOf(totalCount));
        goodNumberCountLabel.setText(String.valueOf(goodAqiCount));
        provinceLabel.setText("%78.23");
        cityLabel.setText("%68.67");
    }

    private static int getGoodAqiCount(ArrayList<StatisticData> statisticDataList) {
        int goodAqiCount = 0;

        for (StatisticData data : statisticDataList) {
            RowAqiData rowAqiData = data.getRowAqiData();
            AccurateAqiData accurateAqiData = data.getAccurateAqiData();
            if (rowAqiData == null || accurateAqiData == null) {
                continue;
            }
            if (accurateAqiData.getAccurateAqiLevel() != null &&
                    accurateAqiData.getAccurateAqiLevel().ordinal() < 3) {
                goodAqiCount++;
            }
        }
        return goodAqiCount;
    }

}
